// pages/transfer/tansferRecord/tansferRecord.js
import { getTransferNoteList, submitUnusual, removeOutbound } from "../../../api/tansfer";
import dayjs from "dayjs";
import Notify from '@vant/weapp/notify/notify';
const {getLocation} = require('../../../utils/map.js')

const statusLabelMap = {
  1: '已出库',
  2: '中转中',
  3: '已完成',
};

const statusStyleMap = {
  1: "color: #8E8E8E",
  2: "color: #4A90E2",
  3: "color: green",
};


Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    statusLabelMap,
    statusStyleMap,
    timeStart: dayjs().format('YYYY-MM-DD 00:00:00'),
    timeEnd: dayjs().add(1, 'day').format('YYYY-MM-DD 00:00:00'),
    transferList: [],
    showAbnormalDialog: false,
    currentGoods: {},
    abnormalInfo: ""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadData() {
      this.getTransferNoteList()
    },
    // 获取列表
    getTransferNoteList() {
      const { timeStart, timeEnd } = this.data
      getTransferNoteList({
        timeStart: timeStart,
        timeEnd: timeEnd,
      })
        .then(res => {
          console.log(res);
          if (res.data && res.data.result) {
            this.setData({
              transferList: res.data.result && res.data.result.map(item => {
                return {
                  ...item,
                  startTime: item.startTime ? dayjs(item.startTime).format('YYYY-MM-DD hh:mm:ss') : '-',
                  finishTime: item.finishTime ? dayjs(item.finishTime).format('YYYY-MM-DD hh:mm:ss') : '-',
                }
              }),
            });
          } else {
            this.setData({
              transferList: [],
            });
          }
        })
        .finally(() => {
        });
    },
    handledDateRange(event) {
      const {start, end} = event.detail
      this.setData({
        timeStart: start,
        timeEnd: end
      })
      console.log(start, end)
    },
    onSubmitAbnormal(event) {
      const {goods} = event.currentTarget.dataset
      this.setData({
        showAbnormalDialog: true,
        currentGoods: goods
      })
    },
    async submitAbnormal() {
      const { longitude, latitude } = await getLocation()
      const { abnormalInfo, currentGoods } = this.data
      submitUnusual({
        orderId: currentGoods.orderId,
        orderDetailId: currentGoods.orderDetailId,
        abnormalContent: abnormalInfo,
        longitude,
        latitude,
      })
        .then(res => {
          if (res.data.code == 200) {
            Notify({
              message: '提交成功',
              type: 'success'
            })
          } else {
            Notify({
              message: res.data.msg || '失败，请重试',
              type: 'danger'
            })
          }
        })
        .catch(err => {
          Notify({
            message: err.msg || '失败，请重试',
            type: 'danger'
          })
        });
    },
    onRemoveCake(event) {
      const {goods} = event.currentTarget.dataset
      const params = {
        orderDetailId: goods.orderDetailId
      }
      removeOutbound(params).then(res => {
        if (res.data.code == 200) {
          Notify({
            message: '移除成功',
            type: 'success'
          })
          this.loadData()
        } else {
          Notify({
            message: res.data.msg || '失败，请重试',
            type: 'danger'
          })
        }
      })
    },
    // 页面相关事件处理函数
    onPullDownRefresh() {
      this.loadData()
      wx.stopPullDownRefresh()
    },
  },

  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      const {timeStart, timeEnd} = this.data
      const dateRangeCom = this.selectComponent('#dateRange')
      dateRangeCom.setDefaultDate([timeStart, timeEnd])
      this.getTransferNoteList()
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})
